package com.example.springboot.service.impl;;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.Book;
import com.example.springboot.entity.Borrow;
import com.example.springboot.entity.Collect;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.BorrowMapper;
import com.example.springboot.mapper.CollectMapper;
import com.example.springboot.service.ICollectService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService{

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BorrowMapper borrowMapper;

    @Override
    public boolean save(Collect entity) {
        // 判断是否重复收藏，收藏了删除之前的收藏，页面显示未收藏，没收藏新增，显示已收藏
        Collect one = super.getOne(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId, entity.getUserId()).eq(Collect::getBookId, entity.getBookId()));
        if (null != one){
            super.remove(new LambdaQueryWrapper<Collect>().eq(Collect::getBookId,entity.getBookId()).eq(Collect::getUserId,entity.getUserId()));
            throw new ServiceException("已取消收藏");
        }
        entity.setTime(DateUtil.now());
        return super.save(entity);
    }

    @Override
    public boolean updateById(Collect entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Collect entity) {
        return super.removeById(entity);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return super.removeBatchByIds(list);
    }

    @Override
    public List<Collect> list() {
        return super.list();
    }

    @Override
    public Collect getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public <E extends IPage<Collect>> E page(E page, Wrapper<Collect> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    public List<Collect> myCollect() {
        User currentUser = TokenUtils.getCurrentUser();
        List<Collect> list = super.list(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId, currentUser.getId()));
        list.stream().forEach(collect -> {
            Book book = bookMapper.selectById(collect.getBookId());
            collect.setBook(book);
            collect.setCollectCount(super.count(new LambdaQueryWrapper<Collect>().eq(Collect::getBookId,book.getId())));
        });
        return list;
    }

}
